package com.sgb.trip.view.activity;

import android.text.TextUtils;
import android.view.View;

import androidx.lifecycle.ViewModelProvider;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sgb.link.entity.EventConstant;
import com.sgb.link.entity.MainEvent;
import com.sgb.link.router.PathTrip;
import com.sgb.link.utils.RoundDialogHelper;
import com.sgb.trip.R;
import com.sgb.trip.databinding.TripActivityAddPassenageBinding;
import com.sgb.trip.entity.PassengerInfoEntity;
import com.sgb.trip.viewmodel.FlightAddPassengerModel;
import com.swgk.core.base.model.entity.RouterKey;
import com.swgk.core.common.base.BaseActivity;
import com.swgk.core.util.MToast;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

/**
 * Time:2021/10/9
 * Author:黄淋
 * Description:添加和编辑乘机人
 */
@Route(path = PathTrip.FlightAddPassengerActivity)
public class FlightAddPassengerActivity extends BaseActivity<FlightAddPassengerModel, TripActivityAddPassenageBinding> {
    @Autowired(name = RouterKey.BOOLEAN, desc = "是否是编辑乘客信息")
    boolean isEditPassenger;

    @Autowired(name = RouterKey.DATA, desc = "乘客信息")
    PassengerInfoEntity passengerInfoEntity;

    Map<String, Object> map;

    @Override
    protected int layoutId() {
        return R.layout.trip_activity_add_passenage;
    }

    @Override
    protected void createViewModel() {
        mViewModel = new ViewModelProvider(this).get(FlightAddPassengerModel.class);
    }

    @Override
    protected void init() {
        ARouter.getInstance().inject(this);
        map = new HashMap<>();
        if (isEditPassenger) {
            mBinding.actionBar.setTitle("编辑乘机人");
            mBinding.tvDeletePassenger.setVisibility(View.VISIBLE);
            mBinding.tvConfirmPassenger.setText("保存");
            if (passengerInfoEntity != null) {
                map.put("id", passengerInfoEntity.getId());
                mBinding.itvInputName.setContent(passengerInfoEntity.getName());
                mBinding.contentEdtIdCard.setText(passengerInfoEntity.getIdCard());
                mBinding.contentEdtPhone.setText(passengerInfoEntity.getTelephone());
            }
        } else {
            mBinding.actionBar.setTitle("新增乘机人");
            mBinding.tvDeletePassenger.setVisibility(View.GONE);
            mBinding.tvConfirmPassenger.setText("确定");
        }

        mBinding.actionBar.setOnBackClickListener(this);
        mBinding.tvConfirmPassenger.setOnClickListener(this);
        mBinding.tvDeletePassenger.setOnClickListener(this);

        mViewModel.mToast.observe(this, data -> MToast.showToast(this, data));
        mViewModel.ldAddData.observe(this, data -> {
            if (data != null) {
                if (isEditPassenger) {
                    EventBus.getDefault().post(MainEvent.make().setMessageTag(EventConstant.EVENT_TRIP_EDIT_PASSENGER_REFRESH_ORDER));
                } else {
                    ARouter.getInstance().build(PathTrip.FlightChoosePassengerActivity).withBoolean(RouterKey.BOOLEAN, true).navigation();
                }
                finish();
            }

        });
        mViewModel.ldDeleteData.observe(this, data -> {
            if (!TextUtils.isEmpty(data)) {
                MToast.showToast(this, data);
                ARouter.getInstance().build(PathTrip.FlightChoosePassengerActivity).withBoolean(RouterKey.BOOLEAN, true).navigation();
                finish();
            }
        });
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.iv_back_red) {//返回
            if (!TextUtils.isEmpty(mBinding.itvInputName.getContent()) || !TextUtils.isEmpty(mBinding.contentEdtIdCard.getText()) || !TextUtils.isEmpty(mBinding.contentEdtPhone.getText())) {
                if (isEditPassenger && passengerInfoEntity != null) {
                    if (passengerInfoEntity.getName().equals(mBinding.itvInputName.getContent()) && passengerInfoEntity.getIdCard().equals(mBinding.contentEdtIdCard.getText().toString())
                            && passengerInfoEntity.getTelephone().equals(mBinding.contentEdtPhone.getText().toString())) {//未修改内容直接退出
                        finish();
                    } else {
                        RoundDialogHelper.showDialog(FlightAddPassengerActivity.this, "温馨提示", "此操作将丢失当前表单填写内容，是否继续", "取消", "确定", new RoundDialogHelper.DialogFunction() {
                            @Override
                            public void normalRoundDialog() {
                                finish();
                            }
                        });
                    }
                } else {
                    RoundDialogHelper.showDialog(FlightAddPassengerActivity.this, "温馨提示", "此操作将丢失当前表单填写内容，是否继续", "取消", "确定", new RoundDialogHelper.DialogFunction() {
                        @Override
                        public void normalRoundDialog() {
                            finish();
                        }
                    });
                }
            } else {
                finish();
            }
        } else if (view.getId() == R.id.tv_delete_passenger) {//删除乘客
            RoundDialogHelper.showDialog(FlightAddPassengerActivity.this, "提示", "确定要删除该乘机人吗？", "取消", "确定", new RoundDialogHelper.DialogFunction() {
                @Override
                public void normalRoundDialog() {
                    if (passengerInfoEntity != null && !TextUtils.isEmpty(passengerInfoEntity.getId())) {
                        mViewModel.deletePassenger(passengerInfoEntity.getId());
                    }
                }
            });
        } else if (view.getId() == R.id.tv_confirm_passenger) {//保存乘客
            if (TextUtils.isEmpty(mBinding.itvInputName.getContent())) {
                MToast.showToast(FlightAddPassengerActivity.this, "请输入姓名！");
                return;
            } else if (TextUtils.isEmpty(mBinding.contentEdtIdCard.getText())) {
                MToast.showToast(FlightAddPassengerActivity.this, "请输入证件号码！");
                return;
            } else if (TextUtils.isEmpty(mBinding.contentEdtPhone.getText())) {
                MToast.showToast(FlightAddPassengerActivity.this, "请输入联系电话！");
                return;
            } else if (mBinding.contentEdtPhone.getText().toString().trim().length() != 11 || !mBinding.contentEdtPhone.getText().toString().startsWith("1")) {
                MToast.showToast(FlightAddPassengerActivity.this, "请输入正确的联系电话！");
                return;
            }

            //保存乘客信息
            map.put("passengerName", mBinding.itvInputName.getContent());
            map.put("cardNo", mBinding.contentEdtIdCard.getText().toString());
            map.put("cardType", 1);
            map.put("passengerPhone", mBinding.contentEdtPhone.getText().toString());

            mViewModel.savePassenger(map);
        }

    }
}
